package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.Category;
import com.cskaoyan.bean.vo.BrandBean;
import com.cskaoyan.bean.vo.CategoryBean;
import com.cskaoyan.bean.vo.GoodsListBean;
import com.cskaoyan.bean.vo.goodsDetailNean.GoodsDetailBean;
import com.cskaoyan.bean.vo.homeIndex.GoodsCountIndex;
import com.cskaoyan.bean.vo.homeIndex.NewGoods;
import com.cskaoyan.service.wx.WxAuthService;
import com.cskaoyan.service.wx.WxGoodsSrvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author 杨杰
 * @version V1.0
 * @Package com.cskaoyan.controller.wx
 * @date 2020/12/9 9:12
 * @Copyright © 暂时还没有
 * @description
 */
@Controller
@RequestMapping("wx/goods")
public class WxGoodsController {

    @Autowired
    WxGoodsSrvice wxGoodsSrvice;

    @RequestMapping("count")
    @ResponseBody
    public BaseRespVo counts(){
        /*
        返回商品总数量
         */
        Integer goodsCount = wxGoodsSrvice.queryAllCount();
        GoodsCountIndex count = new GoodsCountIndex(goodsCount);
        return BaseRespVo.ok(count);
    }

    @ResponseBody
    @RequestMapping("category")
    public BaseRespVo getCategory(Integer id){

        CategoryBean categoryBean = wxGoodsSrvice.getCategory(id);


        return BaseRespVo.ok(categoryBean);
    }
    @ResponseBody
    @RequestMapping("list")
    public BaseRespVo getList(Integer categoryId,Integer brandId,Integer page,Integer size,
                              String keyword,String sort,String order){

        GoodsListBean goodsList = null;


        if (brandId == null && keyword== null){//依据categoryId查询

            goodsList = wxGoodsSrvice.getListByCategoryId(categoryId, page, size);
        }else if (categoryId == null && brandId !=null){//依据brandId查询

            goodsList = wxGoodsSrvice.getListByBrandId(brandId, page, size);
        }else if (categoryId ==0 && brandId == null){//以keyword为依据查询
            if (keyword == null){
                return BaseRespVo.fail("关键字为空");
            }
            goodsList = wxGoodsSrvice.getListByKeyword(keyword,sort,order);
        }else {
            //参数不够，返回失败显示
            return BaseRespVo.fail("你的参数不够");
        }
        return BaseRespVo.ok(goodsList);
    }


    @RequestMapping("detail")
    @ResponseBody
    public BaseRespVo getDetail(Integer id){
        GoodsDetailBean detail = wxGoodsSrvice.getDetail(id);

        return BaseRespVo.ok(detail);
    }
    @ResponseBody
    @RequestMapping("related")
    public BaseRespVo getRelated(Integer id){
        /*
        根据商品id获取相同category的相似商品
         */
        List<NewGoods> related = wxGoodsSrvice.getRelated(id);
        Map<String,List<NewGoods>> map = new HashMap<>();
        map.put("goodsList",related);

        return BaseRespVo.ok(map);
    }

}
